package test1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-03-07
 * Time: 18:15
 */

public class SingletonTest01 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
        //不能 new 出来 在创建一个 比较他们引用是否相同
        Singleton singleton = Singleton.getInstance();

        //判断是否相等
        System.out.println(instance == singleton); //ture
        System.out.println(instance.hashCode());
        System.out.println(singleton.hashCode());
        //她两的hashCode值也相同
    }
}

//饿汉模式
class Singleton {
    //私有化构造器
    private Singleton(){}

    //2.在内部创建对象实例
    private final static Singleton instance = new Singleton();

    //3.提供一个公用的静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }
}